package ${pkg};

import java.io.File;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSON;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ${servicePkg}.${clzName}Service;
import ${domainPkg}.${clzName};

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jacqui.constant.Constant;
import com.jacqui.core.form.PageResult;
import com.jacqui.core.persistence.SearchFilter;
import com.jacqui.core.utils.JsonReturn;
import com.jacqui.core.utils.Servlets;
import com.jacqui.core.web.BaseController;

import com.jacqui.sys.domain.StRydj;

/**
 * 
 * @ClassName: ${clzName}Controller 
 * @Description: ${tableComment} 
 * @author ilaotou@qq.com
 * This class is generatored by AutoTool
 */
@Controller
@RequestMapping(value = "/${tableName?replace('_','/')}")
public class ${clzName}Controller extends BaseController{

    private static Logger logger = LoggerFactory.getLogger(${clzName}Controller.class);
	
	@Autowired
	public ${clzName}Service ${clzName?uncap_first}Service;
	
	/**
	 * @Description 
	 * @param model
	 * @param request
	 * @return 
	 */
	@RequestMapping(value = "index",params="_m=init")
	public String idxInit(Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		return "";
	} 
	/**
	 * @description 分页查询初始化页面
	 * @param model
	 * @param request
	 * @return
	 */
    @RequestMapping(value = "search",params="_m=init")
	public String searchInit(Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		return "${tableName}_list";
	} 
	/**
     * @description  分页查询
     * @param model
     * @param page 当前页，由页面传入
     * @param request
     * @return
     */
	@RequestMapping(value = "search",params="_m=load")
	@ResponseBody
	public Object searching(Model model,@RequestParam("page") String page,@RequestParam("rows") int rows,ServletRequest request) {
	    StRydj user = (StRydj) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
		if(StringUtils.isBlank(page)){
			page="1";
		}
		List<SearchFilter> filters = Lists.newArrayList();
		filters = SearchFilter.parseToList(Servlets.getParametersStartingWith(request, "filter"));
		PageResult<${clzName}> retObject = new PageResult<>();
		retObject = ${clzName?uncap_first}Service.pagingWithTotal(filters, Integer.valueOf(page), rows);
		return paging(retObject.getList(),retObject.getTotalCount(),page);
	}
	/**
	 * @description 查询列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "find",params="_m=exec")
	@ResponseBody
	public Object find(Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		List<${clzName}> list = Lists.newArrayList();
		//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
		list = ${clzName?uncap_first}Service.findBy(parameters);
		return list;
	}
	/**
	 * @description 查询唯一
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "get",params="_m=exec")
	@ResponseBody
	public Object get(Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
		${clzName} ${clzName?uncap_first} = ${clzName?uncap_first}Service.findUniqueBy(parameters);
		return ${clzName?uncap_first};
	}
	/**
	 * @description 根据属性返回名称,主要用于代码名称转换
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getName",params="_m=exec")
	@ResponseBody
	public String getNameBy(@RequestParam("dm") String dm,Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
		parameters.put("dm",dm);
		${clzName} ${clzName?uncap_first} = ${clzName?uncap_first}Service.findUniqueBy(parameters);
		return ${clzName?uncap_first}.getId();
	}
	/**
	 * @description 初始化增加页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "insert",params="_m=init")
	public String insertInit(Model model,ServletRequest request) {
		return "${tableName}_insert";
	} 
	/**
	 * @description  增加
	 * @param model 
	 * @param bnCompanyInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "insert",params = "_m=exec")
	@ResponseBody
	public Object insert(Model model,${clzName} ${clzName?uncap_first},ServletRequest request){
		Map<String,Object> parameters = Maps.newHashMap();
		JsonReturn json = new JsonReturn();
		try{
			//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
			//${clzName?uncap_first}.setCreateAt(TimeUtils.getDateNow());
			//${clzName?uncap_first}.setCreateBy(user.getUserName());
			//${clzName?uncap_first}.setStatus("1");
			${clzName?uncap_first}Service.insert(${clzName?uncap_first});
		}catch(Exception e){
			e.printStackTrace();
			json.setErrcode("1");
			json.setErrmsg("操作失败");
		}
		return json;  
	}
	/**
	 * @description 初始化修改页面
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update",params="_m=init")
	public String updateInit(@RequestParam String ${keyProperty},Model model,ServletRequest request) {
		Map<String,Object> parameters = Maps.newHashMap();
		//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
		parameters.put("${keyProperty}", ${keyProperty});
		${clzName} entity = ${clzName?uncap_first}Service.findUniqueBy(parameters);
		model.addAttribute("entity", entity);
		return "${tableName}_update";
	} 
	/**
	 * @description 修改
	 * @param model
	 * @param bnCompanyInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "update",params = "_m=exec")
	@ResponseBody
	public Object update(Model model,${clzName} ${clzName?uncap_first},ServletRequest request){
		Map<String,Object> parameters = Maps.newHashMap();
		JsonReturn json = new JsonReturn();
		try{
			//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
			//${clzName?uncap_first}.setUpdateAt(TimeUtils.getDateNow());
			//${clzName?uncap_first}.setUpdateBy(user.getUserName());
			//${clzName?uncap_first}Service.update(${clzName?uncap_first});
		}catch(Exception e){
			e.printStackTrace();
			json.setErrcode("1");
			json.setErrmsg("操作失败");
		}
		return json;  
	}
	/**
	 * @description 删除根据主键
	 * @param id 主键
	 * @param model
	 * @param bnCompanyInfo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete",params = "_m=exec")
	@ResponseBody
	public Object deleteById(@RequestParam("id") String id,Model model,${clzName} ${clzName?uncap_first},ServletRequest request){
		Map<String,Object> parameters = Maps.newHashMap();
		JsonReturn json = new JsonReturn();
		try{
			//TODO 参数化处理
			//${clzName?uncap_first}Service.deleteById(id);
			//User user = (User) WebUtils.getSessionAttribute((HttpServletRequest) request, Constant.SESSION_USER_KEY);
			//${clzName?uncap_first}.setUpdateAt(TimeUtils.getDateNow());
			//${clzName?uncap_first}.setUpdateBy(user.getUserName());
			//${clzName?uncap_first}.setStatus("0");
			//${clzName?uncap_first}Service.update(${clzName?uncap_first});
		}catch(Exception e){
			e.printStackTrace();
			json.setErrcode("1");
			json.setErrmsg("操作失败");
		}
		return json;  
	}
	/**
	 * @description 批量删除
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "bacthdelete",params = "_m=exec")
	@ResponseBody
	public Object bacthdelete(Model model,ServletRequest request){
		Map<String,Object> parameters = Maps.newHashMap();
		JsonReturn json = new JsonReturn();
		try{
			//TODO 参数化处理
			${clzName?uncap_first}Service.deleteBy(parameters);
		}catch(Exception e){
			e.printStackTrace();
			json.setErrcode("1");
			json.setErrmsg("操作失败");
		}
		return json;  
	}
	
	
}
